#!/usr/bin/python
#
# Copyright 2011 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


"""Install/uninstall IE 7+ tests."""


__author__ = 'jasonstredwick@google.com (Jason Stredwick)'


import os

import ie7
from bite_build import utils


DLL_COMMAND = 'regsvr32'
DLL_UNINSTALL_FLAG = '/u'


def Install(test_name, src_root=''):
  _HandleDLL(test_name, src_root, '%s' % DLL_COMMAND, 'install')


def Uninstall(test_name, src_root=''):
  _HandleDLL(test_name, src_root, '%s %s' % (DLL_COMMAND, DLL_UNINSTALL_FLAG),
             'uninstall')


def _HandleDLL(test_name, src_root, sub_command, msg_type):
  if os.name != 'nt':
    print ('[ERROR] Invalid operating system; can\'t %s IE addon test %s' %
           (msg_type, test_name))
    return

  print 'Attempting to %s %s IE addon test.' % (msg_type, test_name)
  test_root = os.path.join(src_root, ie7.TESTS_ROOT, test_name)
  dll = os.path.join(test_root, ie7.DLL_NAME)
  command = '%s %s' % (sub_command, dll)
  results = utils.ExecuteCommand(command)

  if not results[utils.SUCCESS]:
    print '[FAILED]  Unable to %s the %s IE addon test.' % (msg_type,
                                                            test_name)
    print results[utils.OUT]
  else:
    print '[SUCCESS] %s IE addon test successfully %s.' %(test_name, msg_type)
